package file;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;


import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.semanticweb.owlapi.model.OWLClass;

//import com.google.common.primitives.Bytes;

/** Baseado na implementa��o
 * http://rishavrohitblog.blogspot.com.br/2014/02/implementing-custom-writablecomparable.html
 * @author Brasileiro
 *
 */
public class FileSD implements
		WritableComparable<FileSD> {
	private Text  name;
	private IntWritable dataset;
	private OWLClass concept;
	
	public FileSD() {
		set(new Text(), new IntWritable());
	}
	
	public FileSD(Text name, IntWritable dataset, OWLClass concept) {
		set(name, dataset, concept);
	}

	private void set(Text name, IntWritable dataset, OWLClass concept) {
		set(name, dataset);
		this.concept = concept;
	}

	public void set(Text name, IntWritable dataset) {
		this.name = name;
		this.dataset = dataset;
	}

	public Text getName() {
		return name;
	}
	
	public String getNameToString() {
		return name.toString();
	}

	public void setName(Text name) {
		this.name = name;
	}

	public IntWritable getDataset() {
		return dataset;
	}
	
	public Integer getDatasetToInteger() {
		return dataset.get();
	}

	public OWLClass getConcept() {
		return concept;
	}

	public void setConcept(OWLClass concept) {
		this.concept = concept;
	}

	public void setDataset(IntWritable dataset) {
		this.dataset = dataset;
	}
	
	public void readFields(DataInput in) throws IOException {
		name.readFields(in);
		dataset.readFields(in);
		
		byte[] yourBytes = Bytes.readByteArray(in);
		
//		int lenght = in.readInt();
//		byte[] yourBytes = new byte[lenght];
//		in.readFully(yourBytes);
		ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
		ObjectInput byteIn =  new ObjectInputStream(bis);
		try {
			Object o = byteIn.readObject();
			concept = (OWLClass) o;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void write(DataOutput out) throws IOException {
//		out.write(name.getBytes());
//        out.writeInt(dataset.get());
//        
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        ObjectOutput byteOut = null;
//    	byteOut = new ObjectOutputStream(bos);   
//    	byteOut.writeObject(concept);
//    	byte[] yourBytes = bos.toByteArray();
//    	
//    	out.write(yourBytes);
		
		name.write(out);
		dataset.write(out);
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutput byteOut = null;
		byteOut = new ObjectOutputStream(bos);   
		byteOut.writeObject(concept);
		byte[] yourBytes = bos.toByteArray();
		
		Bytes.writeByteArray(out, yourBytes);
	
	}
	
	public int compareTo(FileSD tp) {
		int cmp = name.compareTo(tp.getName());
		if (cmp != 0) {
			return cmp;
		}
		return dataset.compareTo(tp.getDataset());
	}
	
	@Override
	public int hashCode() {
		return name.hashCode() * 163 + dataset.hashCode();
	}
	
	@Override
	public boolean equals(Object o) {
		if (o instanceof FileSD) {
			FileSD tp = (FileSD) o;
			return name.equals(tp.getName()) && dataset.equals(tp.getDataset());
		}
		return false;
	}

}
